//
//  SinaWelcomVC.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/16.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit
import SnapKit

private let bottomMargin:CGFloat = 120

class SinaWelcomVC: UIViewController {
    //MARK: -
    //MARK: - 懒加载控件
    private lazy var iconView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    private lazy var welcomLabel:UILabel = UILabel(title:"欢迎回来", fontSize: 16, textColor: UIColor.darkGray)
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //设置UI界面
        setupUI()
    }
    //MARK: -
    //MARK: - 搭建UI界面
  private  func setupUI(){
    
        //设置背景色
    self.view.backgroundColor = UIColor.white
    
        self.view.addSubview(iconView)
        self.view.addSubview(welcomLabel)
        //设置约束
        iconView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-bottomMargin)
            
    }
        welcomLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(20)
    }
    //隐藏文字
    welcomLabel.alpha = 0
    //设置iconView为圆角
    iconView.layer.cornerRadius = 85*0.5
    iconView.layer.masksToBounds = iconView.layer.cornerRadius > 0
    
    }
    //MARK: -
    //MARK: -自动布局的动画效果在viewDidAppear中执行才会有正确的动画效果
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        //自动布局的动画效果在viewDidAppear中执行才会有正确的动画效果
        startAnimation()
        
    }
    //MARK: -
    //MARK: - 开始动画
    func startAnimation(){
        let offsetY = -(UIScreen.main.bounds.height - bottomMargin - iconView.bounds.height)
        
        self.iconView.snp.updateConstraints { (make) in
            
            make.bottom.equalTo(self.view).offset(offsetY)
        }
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 7, options: [], animations: { 
            //执行动画效果
            //更新约束
            //提前刷新页面
            self.view.layoutIfNeeded()
            }) { (finished)->() in
                //动画效果完成的回调
                UIView.animate(withDuration: 0.5, animations: { 
                    self.welcomLabel.alpha = 1
                    }, completion: { (_) -> () in
                        //print("OK")
                        //显示主页面
//                        UIApplication.shared.keyWindow?.rootViewController = SinaMainTabBarVC()
                        //发送通知,切换根视图控制器
                        NotificationCenter.default.post(name: NSNotification.Name(kChoseRootViewController), object: nil)
                        
                
                
                })
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
